<template>
  <div class="meetingDetail wboxnoborder" id="meetingDetail">
    <el-form ref="settingForm" :model="settingForm" label-width="200px" :rules="settingFormRoles" >
      <el-form-item label="查核干部档案：" prop="userId1">
        <el-select v-model="settingForm.userId1" placeholder="请选择">
          <el-option v-for="(item, index) in userlist" :key="index" :label="item.realName" :value="item.userId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="查核有关事项：" prop="userId2">
        <el-select v-model="settingForm.userId2" placeholder="请选择">
          <el-option v-for="(item, index) in userlist" :key="index" :label="item.realName" :value="item.userId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="征求纪检监察部门意见：" prop="userId3">
        <el-select v-model="settingForm.userId3" placeholder="请选择">
          <el-option v-for="(item, index) in userlist" :key="index" :label="item.realName" :value="item.userId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="线索举报调查：" prop="userId4">
        <el-select v-model="settingForm.userId4" placeholder="请选择">
          <el-option v-for="(item, index) in userlist" :key="index" :label="item.realName" :value="item.userId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="savesetting" :loading="saveLoading">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'nesessarysetting',
  props: ['timestamp'],
  watch: {
    timestamp () {
      this.$refs['settingForm'].resetFields()
      this.loadsetting()
      this.loaduserlist()
    }
  },
  data () {
    return {
      settingFormRoles: {
        userId1: [{ required: true, message: '请选择经办人', trigger: 'blur' }],
        userId2: [{ required: true, message: '请选择经办人', trigger: 'blur' }],
        userId3: [{ required: true, message: '请选择经办人', trigger: 'blur' }],
        userId4: [{ required: true, message: '请选择经办人', trigger: 'blur' }]
      },
      settingForm: {
        userId1: '',
        userId2: '',
        userId3: '',
        userId4: ''
      },
      // userlist: [{realName: '张三', userId: 1}, {realName: '李四', userId: 2}]
      userlist: [],
      saveLoading: false
    }
  },
  methods: {
    savesetting () {
      this.$refs['settingForm'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          let para = {
            userId1: this.settingForm.userId1,
            userId2: this.settingForm.userId2,
            userId3: this.settingForm.userId3,
            userId4: this.settingForm.userId4
          }
          this.$http
            .post(this.$http.defaults.selectionAPI + `api/selection/auth/saveNecessaryItems`, para)
            .then(response => {
              if (response.data.success) {
                this.$message({
                  message: '设置成功',
                  type: 'success'
                })
                this.$emit('closeDialog')
              } else {
                this.$message({
                  message: '设置失败:' + response.data.message,
                  type: 'error'
                })
              }
              this.saveLoading = false
            })
            .catch(error => {
              console.log(error)
              this.$message({
                message: '网络请求失败:' + error,
                type: 'error'
              })
              this.saveLoading = false
            })
        }
      })
    },
    loaduserlist () {
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/selection/auth/getSupervisionPersons`)
        .then(response => {
          if (response.data.success) {
            this.userlist = response.data.result
          } else {
            this.$message({
              message: '读取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '网络读取失败:' + error,
            type: 'error'
          })
        })
    },
    loadsetting () {
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/selection/auth/getNecessaryItems`)
        .then(response => {
          if (response.data.success) {
            if (response.data.result) {
              this.settingForm = response.data.result
            }
          } else {
            this.$message({
              message: '读取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '网络读取失败:' + error,
            type: 'error'
          })
        })
    }
  },
  mounted () {
    this.loadsetting()
    this.loaduserlist()
  }
}
</script>
